﻿using System;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using Plugins.Core.Interfaces;
using Plugins.Interfaces;
using Plugins.Library;
using System.Collections;

namespace Plugins.Controls
{
    public partial class PluginLoadWindow : PluginWindow, IPluginLoadWindow
    {
        private static object _sync = new object();

        private AutoResetEvent _loadHandle = new AutoResetEvent(false);
        private int _count = 0;

        public PluginLoadWindow()
        {
            InitializeComponent();
        }

        public void Popup(bool close)
        {
            Thread loadThread = new Thread(new ThreadStart(() => { OpenWindow(true, close); }));
            loadThread.Start();
            _loadHandle.WaitOne();
        }

        public void LoadConfiguration(IPluginConfigurationHost host)
        {
            lock (_sync)
            {
                LoadHost(host);
            }
        }

        public void UnloadConfiguration(IPluginConfigurationHost host)
        {
            lock (_sync)
            {
                UnloadHost(host);
            }
        }

        public void LoadHost(IPluginConfigurationHost configuration)
        {
            if (CanAdd(_count))
            {
                int index = _count++;
                PluginControlHelper.Action<FlowLayoutPanel>(flowLayoutPanel1, control =>
                {
                    AddToPanel(index, configuration, control);
                });
                if (index == ConfigurationCount - 1)
                {
                    CloseWindow();
                }
            }
        }

        public void UnloadHost(IPluginConfigurationHost configuration)
        {
            if (CanRemove(_count))
            {
                int index = --_count;
                PluginControlHelper.Action<FlowLayoutPanel>(flowLayoutPanel1, control =>
                {
                    RemoveFromPanel(index, configuration, control);
                });
                if (index == 0)
                {
                    CloseWindow();
                }
            }
        }

        private bool CanAdd(int count)
        {
            return count < ConfigurationCount;
        }

        private bool CanRemove(int count)
        {
            return count > 0;
        }

        public void AddToPanel(int index, IPluginConfigurationHost configuration, FlowLayoutPanel control)
        {
            control.Controls.Add(Create(index, configuration));
        }

        public void RemoveFromPanel(int index, IPluginConfigurationHost configuration, FlowLayoutPanel control)
        {
            control.Controls.RemoveAt(control.Controls.IndexOfKey(GetConfigurationName(index, configuration)));
        }

        private PictureBox Create(int index, IPluginConfigurationHost configuration)
        {
            PictureBox picture = new PictureBox();
            picture.BackColor = Color.Transparent;
            picture.Image = PluginBitmapHelper.GetTransparentBitmap(imageList1.Images[0], Color.Black);
            picture.Size = imageList1.ImageSize;
            picture.Name = GetConfigurationName(index, configuration);
            picture.Tag = configuration;
            return picture;
        }

        private static string GetConfigurationName(int index, IPluginConfigurationHost configuration)
        {
            if (configuration != null)
            {
                return string.Format(CultureInfo.InvariantCulture, "{0}({1})", configuration.Host.Name, configuration.Host.Id);
            }
            return string.Format(CultureInfo.InvariantCulture, "{0}", index);
        }

        void PluginLoadWindow_Shown(object sender, EventArgs e)
        {
            _loadHandle.Set();
        }

        public int ConfigurationCount { get; set; }

        public int Progress { get { return _count; } }
    }
}
